package com.homework;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * @author: XiangHu
 * @date: 2025年02月26日 14:08
 * @describe:
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入密码:");
        String password = scanner.nextLine();

        if (Login.login(username, password)) {
            // 获取登录用户的ID
            int cUserId = getUserIdByUsername(username);

            boolean isAdmin = Login.isAdmin(username);

            if (isAdmin) {
                while (true) {
                    System.out.println("1. 添加宠物 2. 更新宠物 3. 删除宠物 4. 查询宠物 0. 退出");
                    int action = scanner.nextInt();
                    scanner.nextLine();

                    switch (action) {

                        case 1:
                            System.out.println("输入宠物信息：");
                            System.out.println("昵称:");
                            String nickname = scanner.nextLine();
                            System.out.println("类型:");
                            String type = scanner.nextLine();
                            System.out.println("年龄:");
                            int age = scanner.nextInt();
                            System.out.println("性别:");
                            String gender = scanner.next();
                            System.out.println("价格:");
                            double price = scanner.nextDouble();
                            Pet.addPet(nickname, type, age, gender, price);
                            break;
                        case 2:
                            System.out.println("输入宠物ID进行更新:");
                            int petId = scanner.nextInt();
                            scanner.nextLine();
                            System.out.println("输入宠物信息：");
                            System.out.println("昵称:");
                            nickname = scanner.nextLine();
                            System.out.println("类型:");
                            type = scanner.nextLine();
                            System.out.println("年龄:");
                            age = scanner.nextInt();
                            System.out.println("性别:");
                            gender = scanner.next();
                            System.out.println("价格:");
                            price = scanner.nextDouble();
                            Pet.updatePet(petId, nickname, type, age, gender, price);
                            break;
                        case 3:
                            System.out.println("输入宠物ID进行删除:");
                            petId = scanner.nextInt();
                            scanner.nextLine();
                            Pet.deletePet(petId);
                            break;
                        case 4:
                            System.out.println("选择排序方式: 1. 按价格 2. 按年龄");
                            String orderBy = (scanner.nextInt() == 1) ? "price" : "age";
                            Pet.viewPets(orderBy);
                            break;
                        case 0:
                            System.out.println("退出系统成功");
                            return;
                        default:
                            System.out.println("无效的选择");
                    }
                }
            } else {
                while (true) {
                    System.out.println("1. 查看宠物 2. 购买宠物 0.退出");
                    int action = scanner.nextInt();
                    scanner.nextLine();

                    switch (action) {
                        case 1:
                            System.out.println("选择排序方式: 1. 按价格 2. 按年龄");
                            String orderBy = (scanner.nextInt() == 1) ? "price" : "age";
                            Pet.viewPets(orderBy);
                            break;
                        case 2:
                            System.out.println("请输入宠物ID进行购买:");
                            int petId = scanner.nextInt();
                            Pet.buyPet(petId, cUserId);
                            break;
                        case 0:
                            System.out.println("退出系统成功");
                            return;
                        default:
                            System.out.println("无效的选择");
                    }
                }
            }
        }
    }

    private static int getUserIdByUsername(String username) {
        String sql = "SELECT id FROM h_user WHERE username = ?";
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {

            ps.setString(1, username);

            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    return rs.getInt("id");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }
}
